! ==============================================================================
! This file is part of CUPP (Community Unified Physics Package) since 2023.
! ==============================================================================

module pbl_ysu_mod

  use cupp_mesh_mod
  use atm_types_mod
  use pbl_const_mod

  implicit none

  private

  public pbl_ysu_init
  public pbl_ysu_final
  public pbl_ysu_run

  type ysu_data_type
    integer :: id
    type(cupp_mesh_type), pointer :: mesh => null()
  contains
    procedure :: init  => ysu_data_init
    procedure :: clear => ysu_data_clear
    final :: ysu_data_final
  end type ysu_data_type

  type(ysu_data_type), allocatable :: ysu_data(:)

contains

  subroutine pbl_ysu_init(namelist, mesh)

    character(*), intent(in) :: namelist
    type(cupp_mesh_type), intent(in) :: mesh(:)

    integer iblk

    call pbl_ysu_final()

    allocate(ysu_data(size(mesh)))
    do iblk = 1, size(mesh)
      call ysu_data(iblk)%init(mesh(iblk))
    end do

  end subroutine pbl_ysu_init

  subroutine pbl_ysu_final()

    if (allocated(ysu_data)) deallocate(ysu_data)

  end subroutine pbl_ysu_final

  subroutine pbl_ysu_run(state, tend, dt_phys, dt_pbl)

    type(atm_state_type), intent(in) :: state
    type(atm_tend_type), intent(inout) :: tend
    real(r8), intent(in) :: dt_phys
    real(r8), intent(in) :: dt_pbl

  end subroutine pbl_ysu_run

  subroutine ysu_data_init(this, mesh)

    class(ysu_data_type), intent(inout) :: this
    type(cupp_mesh_type), intent(in), target :: mesh

    call this%clear()

    this%id = mesh%id
    this%mesh => mesh

  end subroutine ysu_data_init

  subroutine ysu_data_clear(this)

    class(ysu_data_type), intent(inout) :: this

  end subroutine ysu_data_clear

  subroutine ysu_data_final(this)

    type(ysu_data_type), intent(inout) :: this

    call this%clear()

  end subroutine ysu_data_final

end module pbl_ysu_mod